<template>
  <el-dialog title="物料产品选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal=true
             :append-to-body="true"
             width="80%"
             center
  >
    <el-row :gutter="20">
      <!--分类数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="itemTypeName"
            placeholder="请输入分类名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="itemTypeOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--物料数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item label="物料编码" prop="itemCode">
            <el-input
              v-model="queryParams.itemCode"
              placeholder="请输入物料编码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="物料名称" prop="itemName">
            <el-input
              v-model="queryParams.itemName"
              placeholder="请输入物料名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="图号" prop="drawNumber">
            <el-input
              v-model="queryParams.drawNumber"
              placeholder="请输入图号"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table border v-loading="loading" ref="itemTable" :data="itemList"
                  @select="handleSelect" @select-all="handleAllSelect" :row-key="getRowKeys" height="500">
          <el-table-column type="selection" width="55" :reserve-selection="true"  />
          <el-table-column label="物料编码" width="120" align="center" key="itemCode" prop="itemCode">
          </el-table-column>
          <el-table-column label="物料名称" min-width="120" align="left" key="itemName" prop="itemName"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="规格型号" align="left" key="specification" prop="specification"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="单位" align="center" key="unitOfMeasure" prop="unitOfMeasure"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="物料/产品" align="center" key="itemOrProduct" prop="itemOrProduct"
                           :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.mes_item_product" :value="scope.row.itemOrProduct"/>
            </template>
          </el-table-column>
          <el-table-column label="所属分类" align="center" key="itemTypeName" prop="itemTypeName" width="120"/>
          <el-table-column
            v-for="col in columnsConfig"
            :key="col.prop"
            :label="col.label"
            :prop="col.prop"
            :width="col.width"
            :align="col.align"
            v-if="col.show">
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="现有库存" align="center" key="quantityOnhand" prop="quantityOnhand" width="120"
                           fixed="right"/>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {listSumWmstock} from "@/api/mes/md/mdItem";
import {treeselect} from "@/api/mes/md/itemtype";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getColumnsConfigKey} from "@/api/system/columnsconfig";

export default {
  name: "MdItemSingle",
  dicts: ['sys_yes_no', 'mes_item_product'],
  components: {Treeselect},
  props: {
    itemOrProduct: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      showFlag: false,
      // 选中数组
      selectedRows1: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品表格数据
      itemList: null,
      // 部门树选项
      itemTypeOptions: undefined,
      // 部门名称
      itemTypeName: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemCode: undefined,
        itemName: undefined,
        drawNumber: undefined,
        itemOrProduct: this.itemOrProduct,
        itemTypeId: 0,

        enableFlag: 'Y'
      },
      columnsConfig: [],
      // 列信息
      /*columns: [
        { key: 0, label: `物料/产品编码`, visible: true },
        { key: 1, label: `物料/产品名称`, visible: true },
        { key: 2, label: `规格型号`, visible: true },
        { key: 3, label: `单位`, visible: true },
        { key: 4, label: `物料/产品`, visible: true },
        { key: 5, label: `物料分类`, visible: true },
        { key: 6, label: `图号`, visible: true },
        { key: 7, label: `品牌`, visible: true },
        { key: 8, label: `颜色`, visible: true },
        { key: 9, label: `技术要求`, visible: true },
        { key: 10, label: `材料规格`, visible: true },
        { key: 11, label: `材料材质`, visible: true },
        { key: 12, label: `内衬类别`, visible: true },
        { key: 13, label: `类型`, visible: true },
        { key: 14, label: `种类`, visible: true },
        { key: 15, label: `专用厂家`, visible: true },
        { key: 16, label: `创建时间`, visible: true },
        { key: 17, label: `现有库存`, visible: true },
      ]*/
    };
  },
  watch: {
    // 根据名称筛选分类树
    itemTypeName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getItemBase();
  },
  methods: {
    getRowKeys(row){
      return row.itemId;
    },
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      listSumWmstock(this.queryParams).then(response => {
        this.itemList=[];
          this.itemList = response.rows;
          this.total = response.total;
        this.$nextTick(function () {
          if (this.selectedRows1 && this.selectedRows1.length > 0) {
            this.selectedRows1.forEach(row => {
              let data = this.itemList.find(item => item.itemId == row.itemId);
              if (data) {
                this.$refs.itemTable.toggleRowSelection(data, true);
              }
            })
          }
        })
          this.loading = false;
        }
      );
    },
    /** 查询分类下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.itemTypeOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.itemTypeId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.itemCode = undefined;
      this.queryParams.itemName = undefined;
      this.queryParams.drawNumber = undefined;
      this.queryParams.itemTypeId= undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //确定选中
    confirmSelect() {
      if (this.selectedRows1 == null || this.selectedRows1.length == 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected', this.selectedRows1);
      this.showFlag = false;
    },
    getItemBase() {
      getColumnsConfigKey('MdItem_Base').then(res => {
        this.columnsConfig = JSON.parse(res.data);
      })
    },
    handleSelect(selection, row) {
      let index=this.selectedRows1.findIndex(item => item.itemId === row.itemId);
      if(index==-1){
        this.selectedRows1.push(row);
      }else{
        this.selectedRows1.splice(index,1);
      }
    },
    handleAllSelect( selection){

      if(selection.length>0){
        const u= selection.filter(item=>
          !this.selectedRows1.some(selectedItem=>selectedItem.itemId===item.itemId)
        )
        this.selectedRows1=[...this.selectedRows1,...u];
      }else{
        const u= this.selectedRows1.filter(item=>
          !this.itemList.some(selectedItem=>selectedItem.itemId===item.itemId)
        )
        this.selectedRows1=u;
      }
    }
  }
};
</script>
